import { Badge } from "antd";

// 定义接口类型
export interface SystemStats {
  users: {
    total: number;
    active: number;
    inactive: number;
  };
  departments: {
    total: number;
    city: number;
    county: number;
    street: number;
    institution: number;
  };
  dictionaries: {
    total: number;
    types: number;
  };
  todayLogs: {
    total: number;
    success: number;
    error: number;
    avgCostTime: number;
  };
  timerTasks: {
    total: number;
    active: number;
    inactive: number;
  };
  settings: {
    total: number;
    types: number;
    encrypted: number;
  };
  notices: {
    total: number;
    sent: number;
    draft: number;
  };
}

export interface SystemHealth {
  database: {
    status: string;
    connectionCount: number;
    responseTime: number;
    type: string;
    version: string;
    size: string;
    sizeBytes: number;
    tableCount: number;
    connections: {
      total: number;
      active: number;
      idle: number;
    };
  };
  server: {
    uptime: number;
    memory: {
      // Node.js 内存信息
      rss: number;
      heapTotal: number;
      heapUsed: number;
      external: number;
      arrayBuffers: number;

      // 系统内存信息
      systemTotal: number;
      systemFree: number;
      systemUsed: number;

      // 显示用的内存信息
      used: number;
      total: number;
      usagePercent: number;

      // 内存使用率
      processUsagePercent: number;
      heapUsagePercent: number;
    };
    nodeVersion: string;
    platform: string;
    arch: string;
    pid: number;
    cpuInfo: {
      model: string;
      cores: number;
      loadAverage: number[];
    };
  };
  application: {
    status: string;
    version: string;
    environment: string;
  };
}

export interface RealTimeMonitor {
  activityChart: Array<{
    time: string;
    operations: number;
    errors: number;
  }>;
  recentErrors: Array<{
    module: string;
    operation_name: string;
    user_name: string;
    error_msg: string;
    operation_time: string;
  }>;
  taskExecutions: Array<{
    task_name: string;
    execute_time: string;
    status: number;
    cost_time: number;
    error_msg?: string;
  }>;
  activeUsers: Array<{
    user_name: string;
    operation_count: number;
    last_operation: string;
  }>;
}

export interface ConcurrencyMonitor {
  current: {
    concurrency: number;
    successRate: number;
    avgResponseTime: number;
    status: string;
  };
  peak: {
    maxConcurrency: number;
    peakTime: string | null;
  };
  trends: Array<{
    time: string;
    requests: number;
    errors: number;
  }>;
  loadLevel: {
    level: string;
    description: string;
  };
}

export function tableColumns() {
  return [
    {
      title: "任务名称",
      dataIndex: "task_name",
      key: "task_name",
      ellipsis: true,
    },
    {
      title: "执行状态",
      dataIndex: "status",
      key: "status",
      render: (status: number) => (
        <Badge
          status={status === 1 ? "success" : "error"}
          text={status === 1 ? "成功" : "失败"}
        />
      ),
    },
    {
      title: "执行时间",
      dataIndex: "execute_time",
      key: "execute_time",
      render: (time: string) => new Date(time).toLocaleString(),
    },
    {
      title: "耗时",
      dataIndex: "cost_time",
      key: "cost_time",
      render: (time: number) => `${time}ms`,
    },
    {
      title: "错误信息",
      dataIndex: "error_msg",
      key: "error_msg",
      ellipsis: true,
      render: (error: string) => error || "-",
    },
  ];
}


// 获取状态颜色
export const getStatusColor = (status: string) => {
    switch (status) {
      case "healthy":
      case "running":
        return "success";
      case "unhealthy":
      case "error":
        return "error";
      case "warning":
        return "warning";
      default:
        return "default";
    }
  };